package com.software.library_conquer;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by CwT on 15/5/30.
 */
public class LibraryActivity {
    private static LibraryActivity manager;
    private static ArrayList<Point> first;
    private static ArrayList<Point> second;
    private static ArrayList<Point> third;
    private static ArrayList<Point> forth;
    private static ArrayList<Point> fifth;

    public static LibraryActivity getManager(){
        if (manager == null)
            manager = new LibraryActivity();
        return manager;
    }
    public ArrayList<Point> getFirst(){
        if (first == null){
            first = new ArrayList<>();
            first.add(new Point(2457, 1228, 1));   //工具书阅览室
            first.add(new Point(2457, 3072, 2));   //古籍阅览室
            first.add(new Point(1557, 2867, 0));   //网络资源检索
            first.add(new Point(491, 2048, 1));    //主书库
        }
        return first;
    }

    public ArrayList<Point> getSecond(){
        if (second == null){
            second = new ArrayList<>();
            second.add(new Point(2334, 737, 1));   //人文社科左上
            second.add(new Point(2867, 1310, 2));  //人文社科右上
            second.add(new Point(2375, 3358, 0));  //人文社科左下
            second.add(new Point(2867, 2785, 1));  //人文社科右下
            second.add(new Point(1884, 1884, 2));  //自习区上
            second.add(new Point(1884, 2211, 0));  //自习区下
        }
        return second;
    }

    public ArrayList<Point> getThird(){
        if (third == null){
            third = new ArrayList<>();
            third.add(new Point(2334, 737, 1));   //科技图书左上
            third.add(new Point(2867, 1310, 2));  //科技图书右上
            third.add(new Point(2375, 3358, 0));  //科技图书左下
            third.add(new Point(2867, 2785, 1));  //科技图书右下
            third.add(new Point(1884, 1884, 1));  //自习区上
            third.add(new Point(1884, 2211, 2));  //自习区下
        }
        return third;
    }

    public ArrayList<Point> getForth(){
        if (forth == null){
            forth = new ArrayList<>();
            forth.add(new Point(2334, 737, 1));   //期刊图书左上
            forth.add(new Point(2867, 1310, 2));  //期刊图书右上
            forth.add(new Point(2375, 3358, 0));  //期刊图书左下
            forth.add(new Point(2867, 2785, 1));  //期刊图书右下
            forth.add(new Point(1884, 1884, 2));  //自习区上
            forth.add(new Point(1884, 2211, 0));  //自习区下
        }
        return forth;
    }

    public ArrayList<Point> getFifth(){
        if (fifth == null){
            fifth = new ArrayList<>();
            fifth.add(new Point(2621, 2621, 1));
            fifth.add(new Point(2621, 1433, 2));
        }
        return fifth;
    }

    /*
    tableID from 0 to 22
     */
    public void freshdensity(ArrayList<Point> arrayList, int index){
        int start = 0, end = 0;
        int count = arrayList.size();
        switch (index){
            case 1:
                start = 0;
                end  = 3;
                break;
            case 2:
                start = 4;
                end = 9;
                break;
            case 3:
                start = 10;
                end  = 15;
                break;
            case 4:
                start = 16;
                end = 21;
                break;
            case 5:
                start = 22;
                end = 23;
                break;
        }
        String url = "https://api.leancloud.cn/1.1/classes/Table"+
                "?where={\"tableId\":{\"$gte\":"+start+
                ",\"$lte\":"+end+"}}";
        HashMap<String, String> argument = new HashMap<>();
        argument.put("X-AVOSCloud-Application-Id", "n9zm8d0zdqh2tzqulf3oxjqh4luhmef7bgumyvtt0gk4pr7s");
        argument.put("X-AVOSCloud-Application-Key", "zp1vsfm2wjyupqov9ctf110gccw323nybvvqn38wtr4ddbk8");
        String response = WebClient.get(url, argument);
        if (response == null)
            return;
        try {
            JSONObject object = new JSONObject(response);
            JSONArray array = object.getJSONArray("results");
            for (int i = 0; i < count; i++){
                JSONObject item = array.getJSONObject(i);
                Point point = arrayList.get(i);
                point.setObjectID(item.getString("objectId"));
                point.setUpdateAt(item.getString("updatedAt"));
                point.setDensity(item.getInt("density"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
    }

    public boolean update(String objectID, int density){
        String url = "https://api.leancloud.cn/1.1/classes/Table/" +objectID;
        String content = "{\"density\":"+density+"}";
        Log.d("cc", content);
        HashMap<String, String> argument = new HashMap<>();
        argument.put("X-AVOSCloud-Application-Id", "n9zm8d0zdqh2tzqulf3oxjqh4luhmef7bgumyvtt0gk4pr7s");
        argument.put("X-AVOSCloud-Application-Key", "zp1vsfm2wjyupqov9ctf110gccw323nybvvqn38wtr4ddbk8");
        argument.put("Content-Type", "application/json");
        String response = WebClient.put(url, argument, content);
        if (response == null)
            return false;
        return true;
    }
}
